<?php
// +--------------------------------------------------------------------------
// | ISDCE [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2017 载攸道 版权所有 <TEL:086-010-57209502>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * [ 后台模块 ]
 */
namespace Admin\Controller;
use Common\Controller\AdminController;

/**
 * 用户群组
 */
class GroupController extends AdminController{
	
	
	/**
	 * 用户群组授权
	 */
	public function Auth(){
		
		action_log('admin_group_auth');//行为记录
		

		//页面配置
		$page = array(
			
			'title'=>'群组权限',
			'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'author'=>'YiPHP',
			'robots' => 'all',
		
		);
		
		$this->assign('page',$page);
		
		//实例化公共用户群组模型
		$m_common_user_group = M('common_user_group');
		$m_common_user = M('common_user');
		$m_common_menu = M('common_menu');
		
	
		//获得所有菜单
		$ammap['id'] = I('param.gid');
		
		$amdata  = $m_common_user_group->where($ammap)->find();
	
	
		$cmmap['status'] =1;
		
		$channel_menu  = $m_common_menu->where($cmmap)->order('sort desc')->select();
		

		//生成树桩结构
		foreach($channel_menu as $i=>$k){
			
			$channel_menu_map['fmid'] =  $channel_menu[$i]['id'];
			$channel_menu_map['status'] = 1;
			
			$data_channel_menu = $m_common_menu->where($channel_menu_map)->count();
			
			//判断是否有下级
			if($data_channel_menu != 0){
				
				$channel_menu_map['fmid'] =  $channel_menu[$i]['id'];
				$channel_menu_map['status'] = 1;
				
				$list_data_channel_menu = $m_common_menu->where($channel_menu_map)->select();
				
				foreach($list_data_channel_menu as $il=>$kl){
					
					$cc_channel_menu_map['fmid'] =  $list_data_channel_menu[$i]['id'];
					$cc_channel_menu_map['status'] = 1;
			
					$cc_data_channel_menu = $m_common_menu->where($cc_channel_menu_map)->count();

				}
				

			}else{
				
				$channel_menu[$i]['top'] = 1;
			}
			

		}
		
		
		//反序列化信息
		$menu = unserialize($amdata['auth']);
		
		//判断是否有权限
		foreach($channel_menu as $i=>$k){
			
			foreach($menu as $ii=>$ik){
				
				if($channel_menu[$i]['id']==$menu[$ii]){
					
					
					$channel_menu[$i]['ok'] = 'checked';
					
				}
				
			}
			
		}
		
		//生成树桩结构
		$channel_menu = find_child($channel_menu);
		
		$channel_menu = _findChildren($channel_menu,0);
		
		//赋值
		$this->assign('channel_menu',$channel_menu);
		$this->assign('node_list',$channel_menu);

		$this->assign('gid',I('param.gid'));
		$this->assign('amdata',$amdata);
	
		$this->display();

	}
	
	/**
	 * 用户群组权限编辑
	 */
	public function AuthEdit(){
		
		action_log('admin_group_authedit');//行为记录
		
		if(IS_POST){
			
			$menu = I('param.menu','','');
			$map['id'] = I('param.gid','','');
			
			$data['auth'] = serialize($menu);
			
			$m_common_user_group= M('common_user_group');	
			
			$result = $m_common_user_group->where($map)->save($data);

			if($result){
				
				 $this->success('授权成功',U('Admin/Group/Auth',array('gid' =>$map['id'])));

			}else{

				
				 $this->error('授权失败');
			}

		}else{
			
			//返回信息		
			$info = $info?$info:get_errcode(-2);
	
			$this->ajaxReturn($info);
		}
		

	}

	/**
	 * 用户群组管理
	 */
    public function Manage(){
		
		action_log('admin_group_manage');//行为记录
		
		//页面配置
		$page = array(
			
			'title'=>'群组管理',
			'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'author'=>'YiPHP',
			'robots' => 'all',
		
		);
		
		$this->assign('page',$page);
		
		$m_common_user_group = M('common_user_group');
		$m_common_user = M('common_user');
		
		
		$map['status'] = 1;
		
		$list = $m_common_user_group->where($map)->select();
		
		foreach($list as $i=>$k){
			$group_map['group_id'] = $list[$i]['id'];
			
			$list[$i]['num'] = $m_common_user->where($group_map)->count();
			
		}
		//赋值
		$this->assign('list',$list);
		
		$this->display();
	}
	
	/**
	 * 新增群组
	 */
	public function Create(){
		
		action_log('admin_group_create');//行为记录
		
		//判断是否是ajax请求
		if(IS_AJAX){

			$m_common_user_group = M('common_user_group');
		
			//获得数据
			$data = I();
			
			$data['status'] = 1;
			
			$result = $m_common_user_group->add($data);
			
			//返回信息
			$info = $result?get_errcode(0):get_errcode(-1);
				
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);
	}
	
	/**
	 * 编辑群组
	 */
	public function Edit(){
    
		action_log('admin_group_edit');//行为记录
		
		//判断是否是ajax请求
		if(IS_AJAX){

			$m_common_user_group = M('common_user_group');
		
			$map['id'] = I('param.id');
			
			$data = I('');
			unset($data['id']);
		
			$result = $m_common_user_group->where($map)->save($data);
			
			//返回信息
			$info = $result?get_errcode(0):get_errcode(-1);
		
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);
	}
	
	/**
	 * 删除群组
	 */
	public function Delete($id,$status=0){
		
		action_log('admin_group_delete');//行为记录
		
		//判断是否是ajax请求
		if(IS_AJAX){
				
			$m_common_user_group = M('common_user_group');
		
			$id = $id?$id:I('param.id');
			$status = $status?$status:I('param.status');
			
			if($id){
				
			
				$id = explode(',',$id); 
				
				
				foreach($id as $i=>$k){
					
					
					$m_common_user_group->delete($id[$i]);
					
					
				}
				
				//返回信息
				$info = get_errcode(0);
			
			}else{
				
				$info = get_errcode(10000001);
			}
			
	
		
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);


	}
	
	
}